package cn.sumpu.app.coolsk.biz.impl;

//import org.slf4j.*;
import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;

import cn.sumpu.app.coolsk.biz.*;
import cn.sumpu.app.coolsk.constant.Table;
import cn.sumpu.app.coolsk.dao.SyslogDao;
import cn.sumpu.app.coolsk.domain.*;
import cn.sumpu.app.coolsk.exception.AppBusinessException;
import cn.sumpu.toolkit.common.page.PageModel;
import cn.sumpu.toolkit.common.util.Common;
public class SyslogBizImpl implements SyslogBiz {

//	private final static Logger logger = LoggerFactory.getLogger(InfoBizImpl.class);
	@Autowired
	private IdentifierBiz identifierBiz;
	
	private SyslogDao syslogDao;
	
	public void setSyslogDao(SyslogDao syslogDao) {
		this.syslogDao = syslogDao;
	}

	//添加系统日志
	@Override
	public void addSyslog(Syslog syslog) throws AppBusinessException {
		if(Common.emptyStr(syslog.getId())){ 
			syslog.setId(identifierBiz.generateId(Table.SYSLOG));
		}
		syslogDao.addSyslog(syslog);
	}
	//查询系统日志
	@Override
	public PageModel getSyslogs(String startTime, String endTime, String actionType, Integer pageSize, Integer page)
			throws Exception {
		if (page == null || page < 1) {
			page = 1;
		}
		if (pageSize == null || pageSize < 1) {
			pageSize = 10;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startTime", Common.parseDate(startTime, "yyyy-MM-dd HH:mm:ss"));
		map.put("endTime", Common.parseDate(endTime, "yyyy-MM-dd HH:mm:ss"));
		map.put("actionType", actionType);
		map.put("pageSize", pageSize);
		map.put("startId", (page - 1) * pageSize);
		List<Syslog> syslogList = syslogDao.getSyslogs(map);
		Integer count = getSyslogsCount(startTime, endTime, actionType);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(syslogList);
		pm.setPageSize(pageSize);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + pageSize - 1) / pageSize);
		return pm;
	}
	//查询系统日志总条数
	@Override
	public Integer getSyslogsCount(String startTime, String endTime, String actionType) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startTime", Common.parseDate(startTime, "yyyy-MM-dd HH:mm:ss"));
		map.put("endTime", Common.parseDate(endTime, "yyyy-MM-dd HH:mm:ss"));
		map.put("actionType", actionType);
		return syslogDao.getSyslogsCount(map);
	}

}
